using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Dragging
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            this.pictureBox1.Tag = this.thumbnail1;
            this.pictureBox1.Location = new Point
                (this.thumbnail1.Location.X + this.thumbnail1.Width / 2 - this.pictureBox1.Width / 2, 
                 this.thumbnail1.Location.Y - this.pictureBox1.Height / 2);

            this.pictureBox1.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);
            this.pictureBox1.MouseUp += new MouseEventHandler(pictureBox1_MouseUp);
            this.pictureBox1.MouseMove += new MouseEventHandler(pictureBox1_MouseMove);
        }

        void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            this.mDragging = true;
            this.mMouseDownLocation = e.Location;
        }

        void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            this.mDragging = false;
        }

        void pictureBox1_MouseMove(object sender, MouseEventArgs e)
        {
            if (this.mDragging)
            {
                Point newPosition = Point.Empty;

                newPosition.X += this.pictureBox1.Left + e.X - this.mMouseDownLocation.X;
                newPosition.Y += this.pictureBox1.Top + e.Y - this.mMouseDownLocation.Y;

                if (pictureBox1.Left == newPosition.X && pictureBox1.Top == newPosition.Y)
                {
                    return;
                }

                this.pictureBox1.Location = newPosition;
                
                Control c = ((Control)this.pictureBox1.Tag);
                newPosition.X += this.pictureBox1.Width / 2 - c.Width / 2;
                newPosition.Y += this.pictureBox1.Height / 2;
                c.Location = newPosition;
            }
        }

        private bool mDragging;
        private Point mMouseDownLocation;
    }
}